package com.gec.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.SysCategory;
import com.gec.model.vo.SysCategoryQueryVo;
import com.gec.system.servcie.SysCategoryService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "影视分类")
@RequestMapping("/admin/system/sysCategory")
public class SysCategoryController {

    @Autowired
    private SysCategoryService sysCategoryService;

    //获取全部数据
    @ApiOperation("获取全部分类")
    @GetMapping("findAll")
    public Result findAll() {
        List<SysCategory> list = this.sysCategoryService.list();
        return Result.ok(list);
    }

    //测试删除
    @ApiOperation("根据Id去移除一个分类")
    @DeleteMapping("/removeCategory/{id}")
    public Result removeCategory(@PathVariable Long id) {
        boolean isDel = this.sysCategoryService.removeById(id);
        return isDel ? Result.ok() : Result.fail();
    }

    //分页和条件查询
    @ApiOperation("分页和查询")
    @GetMapping("/{pageIndex}/{pageSize}") //restfull风格
    public Result findCategoryByPageQuery(@PathVariable Long pageIndex,
                                     @PathVariable Long pageSize,
                                     SysCategoryQueryVo sysCategoryQueryVo ) {
        System.out.println("角色分页查询.....");
        //封装分页的条件
        IPage<SysCategory> page1 = new Page<>(pageIndex, pageSize);
        //分页查询之后的结果
        page1 = this.sysCategoryService.selectPage(page1, sysCategoryQueryVo);
        return Result.ok(page1);
    }

    //添加分类
    @ApiOperation("添加")
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody SysCategory sysCategory) {
        boolean res = this.sysCategoryService.save(sysCategory);
        return res ? Result.ok() : Result.fail();
    }

    //修改
    //根据Id获取当前分类
    @ApiOperation("修改")
    @GetMapping("/findCategoryById/{id}")
    public Result findCategoryById(@PathVariable Long id) {
        SysCategory byId = this.sysCategoryService.getById(id);
        return Result.ok(byId);
    }
    //实现修改
    @PostMapping("/updateCategory")
    public Result updateCategory(@RequestBody SysCategory sysCategory) {
        boolean isup = this.sysCategoryService.updateById(sysCategory);
        return isup ? Result.ok() : Result.fail();
    }

    //删除
    @ApiOperation("删除")
    @DeleteMapping("/removeCategoryByIds")
    public Result removeCategoryByIds(@RequestBody List<Long> ids){
        boolean isDel = this.sysCategoryService.removeByIds(ids);
        return isDel? Result.ok() : Result.fail();
    }


}
